package com.polaris.live.widget

import android.content.Context
import android.text.TextUtils
import android.util.AttributeSet
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import com.bumptech.glide.Glide
import com.polaris.live.databinding.WidgetLiveListHeadBinding
import com.polaris.live.utils.image.loadAvatar

/**
 * LiveListHeadVIew
 *
 * @author Created by 半仙 on 2024/3/11/011 10:19
 */
class LiveListHeadView(context: Context, attrs: AttributeSet? = null) :
    LinearLayout(context, attrs) {

    private val mBinding =
        WidgetLiveListHeadBinding.inflate(LayoutInflater.from(context), this, true)

    private val mViewList = mutableListOf<ImageView>()

    init {
        //ui确定最多展示6个  所以直接写死
        mViewList.add(mBinding.viewHead1)
        mViewList.add(mBinding.viewHead2)
        mViewList.add(mBinding.viewHead3)
        mViewList.add(mBinding.viewHead4)
        mViewList.add(mBinding.viewHead5)
        mViewList.add(mBinding.viewHead6)

        if (resources.configuration.layoutDirection == View.LAYOUT_DIRECTION_RTL) {
            val lp = mBinding.root.layoutParams as LayoutParams
            lp.gravity = Gravity.END or Gravity.CENTER_VERTICAL
            mBinding.root.layoutParams = lp
        }
    }

    fun setList(siteUsers: List<String>?) {
        if (siteUsers?.isNotEmpty() == true) {

            mViewList.forEachIndexed { index, imageView ->
                val item = siteUsers.getOrNull(index)
                if (!TextUtils.isEmpty(item)) {
                    Glide.with(context)
                        .loadAvatar(item)
                        .circleCrop()
                        .into(imageView)

                    imageView.visibility = VISIBLE
                } else {
                    imageView.setImageResource(0)
                    imageView.visibility = GONE
                }
            }

            visibility = VISIBLE
        } else {
            visibility = GONE
        }
    }

}